<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of InStock
 *
 * @author Administrator
 */
class InStock extends CI_Controller
{

    var $users;
    var $whsCode;

    function __construct()
    {
        parent::__construct();
        $gp = gp();
        $userData = $this->session->userdata('users');
        $this->users = $userData;
        if (empty($userData['user_main_whs_code']))
        {
            redirect(blang_url());
        }
        $this->whsCode = $userData['user_main_whs_code'];
        $this->lang->load('wms');
        //加载模型
        $this->load->service('/wms/inventory_service');
        $this->load->service('/wms/mt_server');
        $this->load->service('/wms/warehouse_list_service');
        $this->load->service('/wms/zcsq_order_service');
        $this->load->model('/wms/api/get_product_info_from_erp');
    }
    function check_sku($sku)
    {
         $sku_info = $this->get_product_info_from_erp->get_info($sku);
        if (count($sku_info))
        {
            echo '{"success":true}';
        }
        else
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20024') . '"}';
        }
    }
    function n_stock()
    {
        $gp = gp();
        $sku = strtoupper(trim($gp['sku']));
        $in_out_type = $gp['in_out_type'];
        $comments = trim($gp['comments']);
        $whsCode = trim($gp['whs_code']);
        $quantity = trim($gp['quantity']);
        //验证数据
        $all_clean = FALSE;
        $msg = '';
        if ('' == $whsCode)
        {
            $msg.=lang('wms_tips_20002');
        }
        if ('' == $in_out_type)
        {
            $msg.=lang('wms_tips_20003') . '&';
        }
        if ('' == $sku)
        {
            $msg.=lang('wms_tips_20004') . '&';
        }
        if ($quantity < 1)
        {
            $msg.=lang('wms_tips_20005') . '&';
        }
        if ('' === $msg)
        {
            $all_clean = TRUE;
        }
        if ($all_clean === FALSE)
        {
            echo '{"success":false,"msg":"' . $msg . '"}';
            exit;
        }
        $operator = $this->users['account'];

        $inser = $this->inventory_service->stock_one_t($whsCode, $sku, $quantity, $refNum = '', $comments, $in_out_type, $operator);
        if ($inser)
        {
            echo '{"success":true,"msg":"' . $whsCode . '/' . $sku . '/' . $quantity . '"}';
        }
        else
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_failed') . '"}';
        }
    }

    function m_out()
    {
        $gp = gp();
        $whs_code = $gp['whs_code'];
        $in_out_type = $gp['in_out_type'];
        $pid = trim($gp['pid']);
        $sku = strtoupper(trim($gp['sku']));
        $quantity = $gp['quantity'];
        $ask_staff = trim($gp['ask_staff']);
        $ref_num = trim($gp['ref_num']);
        $comments = trim($gp['comments']);

        //验证数据
        $all_clean = FALSE;
        $msg = '';

        if ('' == $whs_code)
        {
            $msg.=lang('wms_tips_20002') . '&';
        }
        if ('' == $in_out_type)
        {
            $msg.=lang('wms_tips_20003') . '&';
        }
        if ('' == $sku)
        {
            $msg.=lang('wms_tips_20004') . '&';
        }
        if ($quantity < 1)
        {
            $msg.=lang('wms_tips_20005') . '&';
        }
        if ('' == $ask_staff)
        {
            $msg.=lang('wms_tips_20006') . '&';
        }
        if ('Y15' == $in_out_type || 'Y19' == $in_out_type)
        {
            if ('' == $pid)
            {
                $msg.=lang('wms_tips_20007') . '&';
            }
            if ('' == $ref_num)
            {
                $msg.=lang('wms_tips_20008') . '&';
            }
        }
        if ('' === $msg)
        {
            $all_clean = TRUE;
        }
        if ($all_clean)
        {
            $skuArray = array(
                array(
                    'sku' => $sku,
                    'whsCode' => $whs_code,
                    'quantity' => $quantity,
                    'refNum' => $ref_num . '-' . $pid,
                    'comments' => $comments,
                    'in_out_type' => $in_out_type,
                    'pid' => $pid,
                    'ask_staff' => $ask_staff,
                    'operator' => $this->users['account']
                )
            );
            $sucess = $this->inventory_service->out_stock_array_t($skuArray);
            if ($sucess)
            {
                echo '{"success":true,"msg":"' . $whsCode . '/' . $sku . '/' . $quantity . '"}';
            }
            else
            {
                echo '{"success":false,"msg":"' . lang('wms_tips_20009') . '!"}';
            }
        }
        else
        {
            echo '{"success":false,"msg":"' . $msg . '"}';
        }
    }

    function pid_out()
    {
        $gp = gp();
        $pid = trim($gp['pid']);
        $comments = trim($gp['comments']);
        $pid_info = $this->get_product_part($pid, $retun_array = true);
        //验证数据
        if (is_array($pid_info))
        {
            $info = $pid_info['info'];
            $list = $pid_info['list'];
            $skuArray = array();
            foreach ($list as $key => $sku_info)
            {
                $skuArray[] =
                        array(
                            'sku' => $sku_info['sku'],
                            'whsCode' => $sku_info['whs_code'],
                            'quantity' => $sku_info['quantity'],
                            'refNum' => $info['order_num'] . '-' . $pid,
                            'comments' => $comments,
                            'in_out_type' => $sku_info['type'],
                            'pid' => $pid,
                            'ask_staff' => $info['technician'],
                            'operator' => $this->users['account']
                );
            }
            $sucess = $this->inventory_service->out_stock_array_t($skuArray);
            if ($sucess)
            {
                //通知OMS已经出库出库
                $this->mt_server->part_out_by_pid($pid);
                echo '{"success":true,"msg":"' . $pid . '"}';
            }
            else
            {
                echo '{"success":false,"msg":"' . lang('wms_tips_20009') . '!"}';
            }
        }
    }

    function get_product_part($pid='', $retun_array=false)
    {
        $gp = gp();
        $pid = $pid != '' ? $pid : $gp['pid'];
        $sku = $gp['sku'];
        $rs = $this->mt_server->get_mt_part_info($pid, $sku);
        if ($rs && !is_numeric($rs))
        {
            $part_info_array = array();
            $part_info_array['info'] = array(
                'pid' => $rs[0]['pid'],
                'order_num' => $rs[0]['order_num'],
                'oid' => $rs[0]['oid'],
                'status_id' => $rs[0]['status_id'],
                'technician' => $rs[0]['technician']
            );
            foreach ($rs as $key => $part_info)
            {
                if (!in_array($part_info['status_id'], array(8,9)))
                {
                    echo '{"success":false,"msg":"' . lang('wms_tips_20023') . '"}';
                    exit;
                }
                elseif (1 != $part_info['status']||''== $part_info['whs_code'])
                {
                    echo '{"success":false,"msg":"' . $part_info['sku'] . '这个缺货配件还没通知到货,不能操作出库."}';
                    exit;
                }
                if ('maintain' == $part_info['type']&&$retun_array)
                {
                    $part_info['type'] = 'Y19';
                }elseif('maintain' == $part_info['type']){
                    $part_info['type'] = '(Y19)维修用配件出库';
                }elseif($retun_array){
                    $part_info['type'] = 'Y15';
                }else
                {
                    $part_info['type'] = '(Y15)维保配件代管仓出库（供应商自己备的配件）';
                }
                $inventory = $this->inventory_service->get_on_hand($part_info['sku'], $part_info['whs_code']);
                $sku_msg = "";
                if ($inventory < $part_info['quantity'])
                {
                    $sku_msg = "库存不足";
                }

                $part_info_array['list'][] = array(
                    'whs_code' => $part_info['whs_code'],
                    'type' => $part_info['type'],
                    'sku' => $part_info['sku'],
                    'id' => $part_info['id'],
                    'status' => $part_info['status'],
                    'msg' => $sku_msg,
                    'inventory' => $inventory,
                    'quantity' => $part_info['quantity']
                );
            }
            $part_info_array['list'] = $this->warehouse_list_service->fields_whs_code($part_info_array['list']);
            if ($retun_array)
            {
                return $part_info_array;
            }
            echo '{"success":true,"data":' . json_encode($part_info_array) . '}';
        }
        elseif (is_numeric($rs))
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20010') . '."}';
            exit;
        }
        else
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20011') . '"}';
            exit;
        }
    }

    function zcsq_in()
    {
        $gp = gp();
        $whs_code = $gp['whs_code'];
        unset($gp['whs_code']);
        $in_out_type = $gp['in_out_type'];
        unset($gp['in_out_type']);
        $comments = trim($gp['comments']);
        unset($gp['comments']);
        $index = $gp['index'];
        unset($gp['index']);
        $sku_array = array();
        for ($i = 1; $i <= $index; $i++)
        {
            $sku_one = array();

            if (!empty($gp['sku' . $i]))
            {
                $sku_one = array(
                    'sku' => strtoupper(trim($gp['sku' . $i])),
                    'quantity' => trim($gp['qty' . $i])
                );
            }
            if (!empty($sku_one))
            {
                array_push($sku_array, $sku_one);
            }
        }
        if (count($sku_array) < 1)
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20005') . '!"}';
            exit;
        }
        $order_info = array(
            'comments' => $comments,
            'created_time' => time(),
            'creator_id' => $this->users['account'],
            'status' => 1,
            'order_from' => 'manual',
            'ref_num' => "",
            'whs_code' => $whs_code,
            'in_or_out' => 'in',
            'in_out_type' => $in_out_type,
        );
        $items = $sku_array;
        $success = $this->zcsq_order_service->add_zcsq_t($order_info, $items);

        if (FALSE !== $sucess)
        {
            echo '{"success":true,"id":"' . $success . '","msg":"' . lang('wms_tips_20012') . ':' . $success . '"}';
        }
        else
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20013') . '!"}';
        }
    }

    function zcsq_out()
    {
        $gp = gp();
        $whs_code = $gp['whs_code'];
        unset($gp['whs_code']);
        $in_out_type = $gp['in_out_type'];
        unset($gp['in_out_type']);
        $comments = trim($gp['comments']);
        unset($gp['comments']);
        $index = $gp['index'];
        unset($gp['index']);
        $sku_array = array();
        for ($i = 1; $i <= $index; $i++)
        {
            $sku_one = array();

            if (!empty($gp['sku' . $i]) && !empty($gp['qty' . $i]))
            {
                $sku_one = array(
                    'sku' => strtoupper(trim($gp['sku' . $i])),
                    'quantity' => trim($gp['qty' . $i])
                );
            }
            if (!empty($sku_one))
            {
                array_push($sku_array, $sku_one);
            }
        }
        if (count($sku_array) < 1)
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20005') . '!"}';
            exit;
        }
        $order_info = array(
            'comments' => $comments,
            'created_time' => time(),
            'creator_id' => $this->users['account'],
            'status' => 1,
            'order_from' => 'manual',
            'ref_num' => "",
            'whs_code' => $whs_code,
            'in_or_out' => 'out',
            'in_out_type' => $in_out_type,
        );
        $items = $sku_array;
        $success = $this->zcsq_order_service->add_zcsq_t($order_info, $items);

        if (FALSE !== $sucess)
        {
            echo '{"success":true,"id":"' . $success . '","msg":"' . lang('wms_tips_20012') . ':' . $success . '"}';
        }
        else
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20013') . '!"}';
        }
    }

    function zcsq_cg()
    {
        $gp = gp();
        $from_whs_code = $gp['from_whs_code'];
        $whs_code = $gp['whs_code'];
        unset($gp['whs_code']);
        $in_out_type = $gp['in_out_type'];
        unset($gp['in_out_type']);
        $comments = trim($gp['comments']);
        unset($gp['comments']);
        $index = $gp['index'];
        unset($gp['index']);
        $sku_array = array();
        for ($i = 1; $i <= $index; $i++)
        {
            $sku_one = array();

            if (!empty($gp['sku' . $i]))
            {
                $sku_one = array(
                    'sku' => strtoupper(trim($gp['sku' . $i])),
                    'quantity' => trim($gp['qty' . $i])
                );
            }
            if (!empty($sku_one))
            {
                array_push($sku_array, $sku_one);
            }
        }
        $order_info = array(
            'comments' => $comments,
            'created_time' => time(),
            'creator_id' => $this->users['account'],
            'status' => 1,
            'order_from' => 'manual',
            'ref_num' => "",
            'from_whs_code' => $from_whs_code,
            'whs_code' => $whs_code,
            'in_or_out' => 'out_in',
            'in_out_type' => $in_out_type,
        );
        $items = $sku_array;
        $success = $this->zcsq_order_service->add_zcsq_t($order_info, $items);

        if (FALSE !== $sucess)
        {
            echo '{"success":true,"id":"' . $success . '","msg":"' . lang('wms_tips_20014') . ':' . $success . '"}';
        }
        else
        {
            echo '{"success":false,"msg":"' . lang('wms_tips_20015') . '!"}';
        }
    }

}

?>
